//
//  WBHomeViewController.swift
//  WeiBo
//
//  Created by 今代科技 on 2017/10/16.
//  Copyright © 2017年 GZY. All rights reserved.
//

import UIKit
// 定义全局变量
private let cellId = "cellId"
class WBHomeViewController: WBBaseViewController {

    private lazy var statusList = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if userLogon {
            setupNavBarItem()
        }
    }
    /// 加载数据
    override func loadData() {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parmes = ["access_token":"2.00IXLaFGXyntdD15205816abiNOuQB"]
      
        WBNetworkManager.shared.request(URLString: urlString, parameters: parmes as [String : AnyObject]) { (json, isSuccess) in
            print(json)

        }
        WBNetworkManager.shared.get(urlString, parameters: parmes, progress: nil, success: { (_, json) in
            print(json)
        }) { (_, error) in
            print("网络请求失败\(error)")
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now()+3) {
            for i in 0..<10 {
                if self.isPullup {
                    // 将数据追加到底部
                    self.statusList.append("上拉\(i)")
                }else {
                    // 下拉
                    self.statusList.insert(i.description, at: 0)
                }
            }
            self.isPullup = false
            self.refreshControl?.endRefreshing()
            self.tableView?.reloadData()
        }
    }
    /// 显示好友
    @objc private func showFriends() {
        print(#function)
        let vc = WBDemoViewController()
        navigationController?.pushViewController(vc, animated: true)
    
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

// MARK: - 数据源方法
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = statusList[indexPath.row]
        return cell
    }
 }

// MARK: - 设置界面
extension WBHomeViewController {
     func setupNavBarItem() {
        //设置导航栏按钮
        //无法设置高亮
//        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
//        let btn: UIButton = UIButton.zy_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
                tableView?.frame.size.height -= (tabBarController?.tabBar.bounds.height)!+64
    }
}
